<template>
  <div :class="className" :style="{height:height,width:width}" />
</template>

<script>
import echarts from 'echarts'
require('echarts/theme/macarons') // echarts 主体
import resize from '../mixins/resize'

export default {
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '300px'
    },
    btDat: {
      type: Array,
      default: () => {
        return []
      }
    },
    title: {
      type: String,
      default: '信息'
    }
  },
  watch: {
    btDat(name, value) {
      this.datas = name
      this.initChart()
    }
  },
  data() {
    return {
      chart: null,
      datas: []
    }
  },
  mounted() {
    this.$nextTick(() => {

    })
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    initChart() {

      let that = this
      this.chart = echarts.init(this.$el, 'macarons')
      this.chart.setOption({
        //  backgroundColor: 'rgba(1,202,217,.2)',
        tooltip: {
          trigger: 'item',
          formatter: '{a} <br/>{b} : {c} ({d}%)'
        },
        legend: {
          left: 'center',
          bottom: '10',
          textStyle: {
            color: '#999999'
          },
          icon: "circle",
          formatter: function (name) {
            for (let i = 0; i < that.datas.length; i++) {
              if (that.datas[i].name === name) {
                return `${name} ${that.datas[i].value}`;
              }
            }
          },
        },
        series: [
          {
            name: '数量占比',
            type: 'pie',
            radius: '65%',
            center: ['50%', '44%'],
            selectedMode: 'single',
            data: this.datas,
            emphasis: {
              itemStyle: {
                shadowBlur: 10,
                shadowOffsetX: 0,
                shadowColor: 'rgba(0, 0, 0, 0.5)'
              }
            },
            itemStyle: {
              normal: {
                color: function (params) {
                  //自定义颜色
                  var colorList = [
                    'rgb(64, 158, 255)', '#5470c6', '#91cc75', '#fac858', '#ee6666', '#73c0de', '#3ba272', '#fc8452', '#9a60b4', '#ea7ccc'
                  ];
                  return colorList[params.dataIndex]
                }
              }
            },
            animationEasing: 'cubicInOut',
            animationDuration: 2600,
            label: {
              formatter: ['{b}: ', '( {c} )'].join(''),
            }
          }
        ]
      })
    }
  }
}
</script>
